﻿using BoardBroker;
using ComLib.ComType;
using ComUIControl;
using CoreBroker;
using KWindow.KPageCore;
using LogLib;
using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

namespace IPCPages
{
    public partial class PageMakePreparations : PageControlBase
    {       
        private Readying _ready = null;
        private BoardControl _board = BoardControl.Instance;
        private ProcessMakePreparations _process = null;

        public PageMakePreparations()
        {            
            InitializeComponent();
            _process = ProcessMakePreparations.Instance;
            SetProcessBase(_process);
        }

        private Button buttonAddShutterTime = null;
        private Button buttonDecShutterTime = null;
        //private Image imageReady = null;
        //private Button buttonReady = null;
        private Label labelBrightness = null;
        int brightnessValue = 0;

        //初始化窗体数据
        public override void InitCtrls()
        {
            base.InitCtrls();          
            buttonAddShutterTime = GetControl<Button>("buttonAddShutterTime") as Button;
            buttonDecShutterTime = GetControl<Button>("buttonDecShutterTime") as Button;
            labelBrightness= GetControl<Label>("labelBrightness") as Label;             
        }

        //初始化窗体数据
        public override void InitData()
        {
            capturerBroker1.PlayingAbort += this.OnPlayerAborted;
            Process.SetValue("Capturer", capturerBroker1);
            brightnessValue = 0;
            if (labelBrightness != null)
            {
                labelBrightness.Text = brightnessValue.ToString();
            }                      
        }

        private void buttonAddShutterTime_Click(object sender, EventArgs e)
        {
            capturerBroker1.AddShutterTime();
			if (labelBrightness != null)
			{
                brightnessValue++;
                labelBrightness.Text = brightnessValue.ToString();
            }
        }

        private void buttonDecShutterTime_Click(object sender, EventArgs e)
        {
            capturerBroker1.DecShutterTime();
            if (labelBrightness != null)
            {
                brightnessValue--;
                labelBrightness.Text = brightnessValue.ToString();
            }            
        }

        /// <summary>
        /// 保存窗体数据(窗体本次做完后, 进入下一步之前)
        /// </summary>
        public override void UpdateData()
        {
            base.UpdateData();
        }

        /// <summary>
        /// 清理窗体数据(窗体本次做完后)
        /// </summary>
        public override void ClearData()
        {
            base.ClearData();
            if (capturerBroker1 != null)
            {
                capturerBroker1.PlayingAbort -= this.OnPlayerAborted;
            }        
        }

        /// <summary>
        /// 窗体关闭时
        /// </summary>
        public override void Close()
        {
            base.Close();
            capturerBroker1.CloseCapturer();
        }

        public override void StartWork()
        {          
            base.StartWork();
            _ready = Process.GetValue<Readying>("Readying");
            _ready.ResultReported += OnDetectResultReported;
            Speak("准备拍照，请稍低头，保持姿态稳定");
        }
         
        public override void AbortWork()
        {
            Log.WriteAsync("由于未检测到人头像特征，拍照预览页面停止服务，拍照流程决定中止。");
            _board.StopWorking();
            _ready.StopWork();
            capturerBroker1.StopCapturer();
        }

        public override void StopWork()
        {
            base.StopWork();
        }

        protected void OnDetectResultReported(object sender, DetectResultReportedEventArgs e)
        {
            if (this.InvokeRequired)
            {
                DetectResultReportedEventHandler handler 
                    = new DetectResultReportedEventHandler(OnDetectResultReported);
                this.Invoke(handler, new object[] {sender, e});
                return;
            }
            try
            {
                RecogOut result = e.RecogRes;
                if (!string.IsNullOrEmpty(e.RecogRes.Correction.Tips))
                {
                    billboard1.Show(panel1, e.RecogRes.Correction.Tips, 10);
                    Speak(e.RecogRes.Correction.Tips);
                }
            }
            catch (Exception ex)
            {
                Log.WriteExceptionAsync(ex.ToString());
                MsgBox.Show(ex.Message);
            }         
        }

        private void OnPlayerAborted(object sender, PlayingAbortEventArgs e)
        {
            string logInfo = $"镜头运行发生了错误:\n{e.Error}";
            Log.WriteAsync(logInfo);
            MsgBox.Show(logInfo);
        }

        private void NotifyClientTakePhotoCancell()
        {
            if (Process.Job != null)
            {
                Process.Job.Cancel("用户选择了取消拍照.");
            }
        }

        protected override void btnBack_Click(object sender, EventArgs e)
        {
            AbortWork();
            base.btnBack_Click(sender, e);
        }

        protected override void btnNext_Click(object sender, EventArgs e)
        {
            base.btnNext_Click(sender, e);
        }

        protected override void btnCancel_Click(object sender, EventArgs e)
        {
            AbortWork();
            NotifyClientTakePhotoCancell();
            base.btnCancel_Click(sender, e);           
        }

        public override void OnCountdownCompleted(object sender, EventArgs e)
        {           
            if(!Process.IsRunning)
            {
                return;
            }
            string logInfo = "倒计时结束，拍照准备动作未完成，取消拍照.";
            Log.WriteAsync(logInfo);
            if (Process.Job != null)
            {
                Process.Job.Cancel(logInfo);
            }
            base.OnCountdownCompleted(sender, e);
        }

    }
}
